package com.clas.liu.myapplication.network;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * kHttp的管理类
 * 单一实例,所以其中的 OkHttpClient 也是单一实例
 * 类名前不加public,因为该类只在自己所在的包使用
 * Created by liu on 2019/2/25.
 */

class NetworkManager {
    private static volatile NetworkManager manager;
    private OkHttpClient client;
    private static final int CONNECT_TIMEOUT =10;
    private static final int READ_TIMEOUT = 10;

    private NetworkManager(){}

    static NetworkManager getInstance(){
        if(manager == null){
            synchronized (NetworkManager.class){
                if(manager == null){
                    manager = new NetworkManager();
                }
            }
        }
        return manager;
    }
    OkHttpClient getClient(){
        if (client == null){
            synchronized (NetworkManager.class){
                if (client == null){
                    client = new OkHttpClient.Builder().
                            connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS).
                            readTimeout(READ_TIMEOUT,TimeUnit.SECONDS).build();
                }
            }
        }
        return client;
    }
}
